package stringhighlight.actions;
//package test.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IEditorPart;	
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
/**
 * Our sample handler extends AbstractHandler, an IHandler base class.
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class HighlightAction extends AbstractHandler {
	/**
	 * The constructor.
	 */
	public HighlightAction() {
	}

	/**
	 * the command has been executed, so extract extract the needed information
	 * from the application context.
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		
		IWorkbenchWindow currentWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		IWorkbenchPage activePage = window.getActivePage(); // null check omitted
		IEditorPart editorPart = activePage.getActiveEditor(); // null check omitted
		ITextEditor textEditor = (ITextEditor) editorPart; // casting check omitted
		IDocument currentDocument = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
		
		int x = 1;
		int length = 0
		;
		try {
			length = currentDocument.getLineLength(x);
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/*The Following Code searches the Active Document and returns the region where the matching 
		  string is found.
		 */
		String tofind = "temp";
		FindReplaceDocumentAdapter documentfinder = new FindReplaceDocumentAdapter(currentDocument);
		
		try {
			                               // String searchText = data.getName() + "="; //$NON-NLS-1$ 
			                             IRegion region = documentfinder
			                                        .find(0, tofind,
			                                                true /* forwardSearch */,
			                                                true /* caseSensitive */,
			                                               true /* wholeWord */, false /* regExSearch */);	
			                             ((ITextEditor) textEditor).selectAndReveal(region.getOffset(), region.getLength());
			                            } catch (BadLocationException e) {
			                            	e.printStackTrace();
			                            }
		String myString = "";
		myString = Integer.toString(length);
		
		MessageDialog.openInformation(
				window.getShell(),
				"Test",
				myString);
		
		return null;
	}
}
}